﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>TimePlans</title>
</head>
<body>
    <div>
        <input type="text" id="txtNum" value="" style="width: 160px;" />
    </div>
    <script src="~/Content/js/jquery-1.8.2.js"></script>
    <script src="~/Content/js/tjzheng.oop.js"></script>
    <script type="text/javascript">
        var TimePlan = Class.create();
        TimePlan.prototype = {
            //构造函数
            initialize: function (targetTime) {
                this.targetTime = targetTime;
                this.targetMilliSecond = (function (_targetTime) {
                    var f = _targetTime.split(' ', 2);
                    var d = (f[0] ? f[0] : '').split('-', 3);
                    var t = (f[1] ? f[1] : '').split(':', 3);
                    return (new Date(
                    parseInt(d[0], 10) || null,
                    (parseInt(d[1], 10) || 1) - 1,
                    parseInt(d[2], 10) || null,
                    parseInt(t[0], 10) || null,
                    parseInt(t[1], 10) || null,
                    parseInt(t[2], 10) || null
                    )).getTime();
                }(this.targetTime));
                this.remainTime = { Day: 0, Hour: 0, Minute: 0, Second: 0 };
                window.TimePlanTemp = this;
            },
            //目标时间
            targetTime: "",
            //剩余时间
            remainTime: {},
            //要执行的任务
            work: function () { },
            //任务结束后事件
            completed: function () { },
            //间隔时间（默认1s）
            perTime: 1,
            //任务ID
            planID: null,
            //执行任务
            executePlan: function () {
                var self = window.TimePlanTemp;
                var startTime = self.targetMilliSecond;
                var currentTime = new Date();
                var result = (startTime - Date.parse(currentTime)) / 1000;
                if (result >= 0) {
                    self.remainTime.Day = parseInt(result / 24 / 3600);
                    self.remainTime.Hour = parseInt((result / 3600) % 24);
                    self.remainTime.Minute = parseInt((result / 60) % 60);
                    self.remainTime.Second = parseInt(result % 60);
                    $plan = self.executePlan;
                    if (!self.planID) {
                        self.planID = setInterval(function () { $plan(); }, self.perTime * 1000);
                    }
                    self.work();
                }
                else {
                    if (self.planID) {
                        clearInterval(self.planID);
                        self.completed();
                    }
                    delete window.TimePlanTemp;
                }
            }
        };

        var objTimePlan = new TimePlan("2014-07-23 16:48:00");
        // objTimePlan.perTime = 2;
        objTimePlan.work = function () {
            var objRemainTime = objTimePlan.remainTime;
            $("#txtNum").val("下班倒计时："/* + objRemainTime.Day + "天" */ + objRemainTime.Hour + "时" + objRemainTime.Minute + "分" + objRemainTime.Second + "秒")
        };
        objTimePlan.completed = function () { alert("下班了！"); };
        objTimePlan.executePlan();
    </script>
</body>
</html>
